package com.lwl.leetcode;

import org.junit.Test;

/**
 * @author lwl
 * @Description 给你链表的头节点 head ，每 k 个节点一组进行翻转，请你返回修改后的链表。
 * <p>
 * k 是一个正整数，它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍，那么请将最后剩余的节点保持原有顺序。
 * <p>
 * 你不能只是单纯的改变节点内部的值，而是需要实际进行节点交换。
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode.cn/problems/reverse-nodes-in-k-group
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 * @date 2023/7/28 16:01
 */
public class Code25ReverseKGroup {
    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    @Test
    public void test2() {
        ListNode node = new ListNode(1);
        ListNode t = node;
        node.next = new ListNode(2);
        node = node.next;
        node.next = new ListNode(3);
        node = node.next;
        node.next = new ListNode(4);
        node = node.next;
        node.next = new ListNode(5);

        ListNode head = reverseKGroup(t, 2);

        while (head != null) {
            System.out.println(head.val);
            head = head.next;
        }
    }

    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode res = null;
        ListNode h = head;
        ListNode curEnd = null;
        ListNode curHead = null;
        ListNode nextHead = null;
        ListNode preTail = null;
        boolean flag = true;
        while ((curEnd = getNextK(h, k)) != null) {
            nextHead = curEnd.next;
            curEnd.next = null;
            curHead = reverseListNode(h);
            if (preTail != null) {
                preTail.next = curHead;
            }
            // reverse之后curHead变成了head，h变成了尾
            if (flag) {
                res = curHead;
                flag = false;
            }
            preTail = h;
            h.next = nextHead;
            h = nextHead;
        }
        return res;
    }

    private ListNode getNextK(ListNode head, int k) {
        ListNode node = head;
        for (int i = 0; i < k - 1; i++) {
            if (node == null) {
                return null;
            }
            node = node.next;
        }
        return node;
    }

    private ListNode reverseListNode(ListNode head) {
        ListNode t = head;
        ListNode pre = null;
        while (t != null) {
            ListNode node = t.next;
            t.next = pre;
            pre = t;
            t = node;
        }
        return pre;
    }

    @Test
    public void test() {
        ListNode node = new ListNode(1);
        ListNode head = node;
        node.next = new ListNode(2);
        node = node.next;
        node.next = new ListNode(3);
        node = node.next;
        node.next = new ListNode(4);
        node = node.next;
        node.next = new ListNode(5);
        head = reverseListNode(head);
        while (head != null) {
            System.out.println(head.val);
            head = head.next;
        }
    }

}
